package com.xone.xapp;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class PurchaseCreateActivity extends BasicImageOptActivity {
	protected final String TAG = PurchaseCreateActivity.class.getSimpleName();
	protected Map<String, String> infoMap = new LinkedHashMap<String, String>();
	protected Spinner spinnerPurchaseTypeName;
    protected LinearLayout outerView;
    protected boolean auth = false;
	protected List<String> purchaseTypeList = new ArrayList<String>();
	@Override
	public String getHeaderTitle() {
		return "求购发布";
	}
	@Override
	public int getContentView() {
		if (!auth) {
			return R.layout.assistent_info;
		}
		return R.layout.purchase_create;
	}
	@Override
	public Context getContext() {
		return this;
	}
	@Override
	public LinearLayout getOuterView() {
		return outerView;
	}
	@Override
	public String getImagesTag() {
		return "IMAGES_TAG";
	}
	@Override
	public String getPostUrl() {
		return "/purchase/createJson.html";
	}

	protected boolean checkAuth() {
		if (UserInfo.clevelUser || UserInfo.blevelUser) {
			return true;
		}
		return false;
	}
	/**
	 * 提交完成后的回调
	 */
	@Override
	public void postFinish(JSONObject result) {
		if (null == result || result.isNull("purchase")) {
			Toast.makeText(getContext(), "操作失败", Toast.LENGTH_SHORT).show();
			return;
		}
		Toast.makeText(getContext(), "提交成功", Toast.LENGTH_SHORT).show();
		this.finish();
	}
	protected void onCreate(Bundle savedInstanceState) {
		auth = checkAuth();
		super.onCreate(savedInstanceState);
		if (!auth) {//没有权限
			TextView view = (TextView)findViewById(R.id.information);
			view.setText("你所在的用户级别不能进行此项操作");
			return;
		}
		outerView = (LinearLayout)findViewById(R.id.purchase_create_details_zone);
		infoMap.put("purchaseName", "求购的产品名称不能为空");
		infoMap.put("purchaseAddress", "求购的产品产地不能为空");
		infoMap.put("purchaseLocation", "求购的产品属地不能为空");
		infoMap.put("purchaseNum", "求购的产品数量不能为空");
		infoMap.put("purchaseDesc", "");
		spinnerPurchaseTypeName = (Spinner)findViewById(R.id.purchaseTypeName);
		purchaseTypeList.add("冻品");
		purchaseTypeList.add("干货");
		purchaseTypeList.add("活鲜");
		purchaseTypeList.add("水果");
		purchaseTypeList.add("调料");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, purchaseTypeList);
		spinnerPurchaseTypeName.setAdapter(adapter);
		Button submitProduct = (Button)findViewById(R.id.submit_purchase);
		submitProduct.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, String> value = checkAndGetValue(infoMap);
				if (value.isEmpty()) {
					return;
				}
				int images = 0;
				View [] imageViews = new View[3];
				for (int i = 0; i < outerView.getChildCount(); i++) {
					View tv = outerView.getChildAt(i);
					String tag = (String)tv.getTag();
					if (getImagesTag().equals(tag)) {
						imageViews[images] = tv;
						images ++;
					}
				}
				if (images == 0) {
					notPass(null, "请选择至少一张图片");
					return;
				}
				showProgress();
				String purchaseType = spinnerPurchaseTypeName.getSelectedItem().toString();
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				for (Map.Entry<String, String> info : value.entrySet()) {
					params.add(new BasicNameValuePair("purchase." + info.getKey(), info.getValue()));
				}
				String va = String.valueOf(purchaseTypeList.indexOf(purchaseType));
				params.add(new BasicNameValuePair("purchase.purchaseType", va));
				new Base64Task(getContext(), params).execute(imageViews);
			}
		});
		Button selectImage = (Button)findViewById(R.id.button_select_image);
		selectImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int images = getImageCount();
				if (images >= 3) {
					Toast.makeText(getContext(), "最多只能选择三张图片", Toast.LENGTH_SHORT).show();
					return;
				}
				Intent intent = new Intent();
				/* 开启Pictures画面Type设定为image */  
                intent.setType("image/*");  
                /* 使用Intent.ACTION_GET_CONTENT这个Action */  
                intent.setAction(Intent.ACTION_GET_CONTENT);   
                /* 取得相片后返回本画面 */  
                startActivityForResult(intent, 1); //imageview_select_image
			}
		});
	}

}
